|
RAIS
3.2
|
00001 00028 using System; 00029 using System.Collections.Generic; 00030 using System.Data; 00031 using System.Text; 00032 using TableTypes = System.Collections.Generic.Dictionary<string, RAIS.Common.TableManagement.Table.TableTypeData>; 00033 using Fields = System.Collections.Generic.Dictionary<string, RAIS.Common.TableManagement.Field>; 00034 00035 namespace RAIS.Common.TableManagement 00036 { 00037 public class Table: DBObject 00038 { 00039 //********************************************************************* 00040 #region Enumerators 00041 public enum TableType 00042 { 00043 Single = 0, 00044 WithHistory, 00045 Authorization 00046 } 00047 #endregion 00048 //********************************************************************* 00049 #region Fields 00050 static TableTypes tableTypesDictionary; 00051 00052 TableType tableType; 00053 bool customTable = true; 00054 bool needsValidation = true; 00055 Fields fields; 00056 Fields addedFields = new Fields(); 00057 Fields removedFields = new Fields(); 00058 bool modified = false; 00059 string queryName; 00060 bool usedInListWithFilter; 00061 #endregion 00062 //********************************************************************* 00063 #region Properties 00064 static public TableTypes TableTypesDictionary 00065 { 00066 get { return tableTypesDictionary; } 00067 set { tableTypesDictionary = value; } 00068 } 00069 00070 public TableType Type 00071 { 00072 get { return this.tableType; } 00073 } 00074 public TableTypeData TypeData 00075 { 00076 get { return tableTypesDictionary[this.TypeText]; } 00077 } 00078 public string TypeText 00079 { 00080 get 00081 { 00082 string result = ""; 00083 foreach (KeyValuePair<string, TableTypeData> tableTypeItem in tableTypesDictionary) 00084 if (tableTypeItem.Value.Type == this.tableType) 00085 { 00086 result = tableTypeItem.Value.VisibleName; 00087 break; 00088 } 00089 return result; 00090 } 00091 set { this.tableType = tableTypesDictionary[value].Type; } 00092 } 00093 public bool CustomTable 00094 { 00095 get { return this.customTable; } 00096 set { this.customTable = value; } 00097 } 00098 public bool NeedsValidation 00099 { 00100 get { return this.needsValidation; } 00101 set { this.needsValidation = value; } 00102 } 00103 public string PrimaryKeyFieldName 00104 { 00105 get { return "PK " + this.name + " ID"; } 00106 } 00107 public string ForeignKeyFieldName 00108 { 00109 get { return "FK " + this.name + " ID"; } 00110 } 00111 public string ValidationTableName 00112 { 00113 get { return this.name + " Validation"; } 00114 } 00115 public string HistoryTableName 00116 { 00117 get 00118 { 00119 return (this.tableType == TableType.WithHistory) 00120 ? this.name + " History" 00121 : string.Empty; 00122 } 00123 } 00124 public string ValidationPrimaryKeyFieldName 00125 { 00126 get { return "PK " + this.ValidationTableName + " ID"; } 00127 } 00128 public string ValidationForeignKeyFieldName 00129 { 00130 get { return "FK " + this.name + " Validation ID"; } 00131 } 00132 public string VisibleTextFieldName 00133 { 00134 get 00135 { 00136 string fieldName = ""; 00137 if (this.Fields != null) 00138 { 00139 foreach (KeyValuePair<string, Field> fieldPair in this.Fields) 00140 { 00141 if (fieldPair.Value.IsNameField) 00142 { 00143 fieldName = fieldPair.Value.Name; 00144 if (fieldPair.Value.IsLookupField) 00145 fieldName = "FK " + fieldName + " ID"; 00146 break; 00147 } 00148 } 00149 00150 if (fieldName == "") 00151 { 00152 // Return the name of the first text field 00153 foreach (KeyValuePair<string, Field> fieldPair in this.Fields) 00154 { 00155 //fieldName = fieldPair.Value.Name; 00156 if ((name.StartsWith("Evaluator") || name.StartsWith("Protector")) 00157 && fieldPair.Value.Type == Field.FieldType.SingleLookup) 00158 { 00159 fieldName = fieldPair.Value.ForeignKeyFieldName; 00160 break; 00161 } 00162 } 00163 } 00164 00165 if (fieldName == "") 00166 { 00167 // Return the name of the first text field 00168 foreach (KeyValuePair<string, Field> fieldPair in this.Fields) 00169 { 00170 //fieldName = fieldPair.Value.Name; 00171 if (fieldPair.Value.Type != Field.FieldType.SingleLookup 00172 && fieldPair.Value.Type != Field.FieldType.MultipleLookup 00173 && fieldPair.Value.Type != Field.FieldType.Label) 00174 { 00175 fieldName = fieldPair.Value.Name; 00176 break; 00177 } 00178 } 00179 } 00180 } 00181 00182 //string fieldName = ""; 00183 //if (this.Fields != null && this.Fields.Count != 0) 00184 //{ 00185 // foreach (KeyValuePair<string, Field> fieldPair in this.Fields) 00186 // { 00187 // Field field = fieldPair.Value; 00188 // if (field.Type == Field.FieldType.SingleLookup) 00189 // fieldName = field.RelatedTable.ForeignKeyFieldName; 00190 // else if (field.Type == Field.FieldType.MultipleLookup) 00191 // fieldName = this.PrimaryKeyFieldName; 00192 // else 00193 // fieldName = fieldPair.Value.Name; 00194 // break; 00195 // } 00196 //} 00197 //else 00198 //{ 00199 // fieldName = this.PrimaryKeyFieldName; 00200 //} 00201 return fieldName; 00202 } 00203 } 00204 public Fields Fields 00205 { 00206 get { return this.fields; } 00207 set { this.fields = value; } 00208 } 00209 public string QueryName 00210 { 00211 get { return (this.queryName == "") ? this.name : queryName; } 00212 set { this.queryName = value; } 00213 } 00214 public string MultipleLookupForeignKey 00215 { 00216 get { return "FK " + this.Name + " ID"; } 00217 } 00218 public bool HistoryTable 00219 { 00220 get { 00221 if (this.Name.Length < Constants.HISTORY_TABLE_SUFFIX.Length + 1) 00222 return false; 00223 else 00224 return (this.Name.Substring( 00225 this.Name.Length - Constants.HISTORY_TABLE_SUFFIX.Length 00226 , Constants.HISTORY_TABLE_SUFFIX.Length) == Constants.HISTORY_TABLE_SUFFIX); 00227 } 00228 } 00229 public bool WithHistoryValidation 00230 { 00231 get 00232 { 00233 return this.tableType == TableType.WithHistory 00234 && this.name != Constants.FACILITY_TABLE_NAME 00235 && this.name != Constants.DEPARTMENT_TABLE_NAME; 00236 } 00237 } 00241 public Fields AddedFields 00242 { 00243 get { return this.addedFields; } 00244 } 00248 public Fields RemovedFields 00249 { 00250 get { return this.removedFields; } 00251 } 00255 public bool Modified 00256 { 00257 get { return this.modified; } 00258 set 00259 { 00260 if (!value) 00261 { 00262 this.RemovedFields.Clear(); 00263 this.AddedFields.Clear(); 00264 } 00265 this.modified = value; 00266 } 00267 } 00268 public bool AddHistoryAllowed 00269 { 00270 get 00271 { 00272 return this.usedInListWithFilter 00273 && (this.tableType == TableType.Single); 00274 } 00275 } 00276 #endregion 00277 //********************************************************************* 00278 #region Public methods 00279 public bool ValidateField(Field field, ref string returnMessage) 00280 { 00281 if (field.Name == null || field.Name.Length == 0) 00282 { 00283 returnMessage = RAIS.Common.Messages.TABLE_FIELD_NAME_MISSING; 00284 return false; 00285 } 00286 return true; 00287 } 00288 public bool ValidateNewField(Field newField, ref string returnMessage) 00289 { 00290 if (newField.Name == null || newField.Name.Length == 0) 00291 { 00292 returnMessage = RAIS.Common.Messages.TABLE_FIELD_NAME_MISSING; 00293 return false; 00294 } 00295 else 00296 if (this.Fields.ContainsKey(newField.Name)) 00297 { 00298 returnMessage = RAIS.Common.Messages.TABLE_FIELD_ALREADY_EXISTS; 00299 return false; 00300 } 00301 else 00302 return true; 00303 } 00304 public string GetForeignKeyFieldNameByTableID(int tableID) 00305 { 00306 string fieldName = ""; 00307 00308 if (this.fields != null) 00309 { 00310 foreach (KeyValuePair<string, Field> kvp in this.fields) 00311 { 00312 Field field = kvp.Value; 00313 00314 if (field.RelatedTable != null 00315 && field.RelatedTable.ID == tableID) 00316 { 00317 fieldName = field.ForeignKeyFieldName; 00318 break; 00319 } 00320 } 00321 } 00322 return fieldName; 00323 } 00324 public bool AddField(Field newField, ref string returnMessage) 00325 { 00326 newField.Table = this; 00327 bool success = this.ValidateNewField(newField, ref returnMessage); 00328 if (success) 00329 { 00330 //if (this.RemovedFields.ContainsKey(newField.Name)) 00331 // this.RemovedFields.Remove(newField.Name); 00332 //else 00333 this.AddedFields.Add(newField.Name, newField); 00334 00335 this.Fields.Add(newField.Name, newField); 00336 00337 this.Modified = true; 00338 } 00339 return success; 00340 } 00341 public bool RemoveField(Field removedField, ref string returnMessage) 00342 { 00343 if (this.AddedFields.ContainsKey(removedField.Name)) 00344 this.AddedFields.Remove(removedField.Name); 00345 else 00346 this.RemovedFields.Add(removedField.Name, removedField); 00347 00348 this.Fields.Remove(removedField.Name); 00349 this.Modified = true; 00350 return true; 00351 } 00352 #endregion 00353 //********************************************************************* 00354 #region Constructors 00355 public Table() 00356 { } 00357 00358 public Table(IDataRecord record) 00359 { 00360 this.id = (int)record["PK Table ID"]; 00361 this.name = record["Table Name"].ToString(); 00362 this.tableType = (TableType)Enum.Parse(typeof(TableType), record["Table Type Name"].ToString()); 00363 this.customTable = (int.Parse(record["Custom Table"].ToString()) == 1); 00364 this.needsValidation = (int.Parse(record["Needs Validation"].ToString()) == 1); 00365 this.queryName = record["Query Name"].ToString(); 00366 try 00367 { 00368 this.usedInListWithFilter = (int.Parse(record["Used In List With Filter"].ToString()) == 1); 00369 } 00370 catch 00371 { 00372 this.usedInListWithFilter = false; 00373 } 00374 } 00375 #endregion 00376 //********************************************************************* 00377 #region Nested classes 00378 public class TableTypeData 00379 { 00380 //********************************************************************* 00381 #region Fields 00382 int id; 00383 string name; 00384 string visibleName; 00385 #endregion 00386 //********************************************************************* 00387 #region Properties 00388 public int ID 00389 { 00390 get { return this.id; } 00391 } 00392 public TableType Type 00393 { 00394 get { return (TableType)Enum.Parse(typeof(TableType), this.name); } 00395 } 00396 public string VisibleName 00397 { 00398 get { return this.visibleName; } 00399 } 00400 #endregion 00401 //********************************************************************* 00402 #region Constructors 00403 public TableTypeData() 00404 {} 00405 public TableTypeData(IDataRecord record) 00406 { 00407 this.id = (int)record["PK Table Type ID"]; 00408 this.name = record["Table Type Name"].ToString(); 00409 this.visibleName = record["Table Type Visible Name"].ToString(); 00410 } 00411 #endregion 00412 } 00413 #endregion 00414 } 00415 }